﻿using Password_Manager.Database;
using Password_Manager.EncryptedFile;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Password_Manager
{
    public partial class OperationRecord : Form
    {
        private int account; // 用户ID
        private string ldentificationCode;
        private string password;
        SimpleXmlHelper xmlHelper = new SimpleXmlHelper();
        CreateDatabaseAndTables create = new CreateDatabaseAndTables();

        public OperationRecord(int id)
        {
            InitializeComponent();
            MessageBox.Show($"当前用户ID：{id}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            account = id; // 设置用户ID
            LoadOperationRecords();



        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)//表格
        {

        }
        public async Task LoadOperationRecords() //绑定数据
        {
            try
            {
                var tables = await create.GetTables(account);

               
                dataGridView1.Columns.Clear();
                // 添加自定义列
                dataGridView1.Columns.Add("LdentificationCode", "识别码");
                dataGridView1.Columns.Add("Account", "账户");
                dataGridView1.Columns.Add("Encryption", "加密信息");
                //dataGridView1.DataSource = tables;
                // 填充数据
                foreach (var table in tables)
                {
                    dataGridView1.Rows.Add(
                        table.LdentificationCode,
                        table.Account,
                        table.Encryption
                    );
                }
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

                dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;

                dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dataGridView1.MultiSelect = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载数据失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) //删除
        {
            DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];

            string code = selectedRow.Cells[0].Value?.ToString();

            if (string.IsNullOrEmpty(code))
            {
                MessageBox.Show("识别码为空，无法删除！", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (MessageBox.Show($"确定删除 {code}？", "警告",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                xmlHelper.DeleteFile(code);   // XML 删除
                create.DeleteTable(code);    // 数据库删除

                LoadOperationRecords();
            }
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddEncryptedFiles add = new AddEncryptedFiles(account);
            add.ShowDialog();
            LoadOperationRecords();
        }

        private void 解码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
            string encryption = selectedRow.Cells[2].Value?.ToString();


            ManagerInterface add = new ManagerInterface(account);

            MessageBox.Show($"当前加密文件为：{encryption}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (encryption == "")
            {
                MessageBox.Show("密码未选中！");
                return;
            }
            add.Decoding(encryption);

        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
            string encryption = selectedRow.Cells[0].Value?.ToString();

            ModifyUser modify = new ModifyUser(account,encryption);
            modify.ShowDialog(); // 显示修改用户信息窗口
            LoadOperationRecords();
        }
    }
}

